var app = getApp();
/* 生成UUID */
function getUUID() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4";
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  s[8] = s[13] = s[18] = s[23] = "-";

  var uuid = s.join("");
  uuid = uuid.replace(/-/g, '');
  return uuid;
}
/*获取文件类型 */
function getMimeType(name) {
  var index = name.lastIndexOf('.');
  return name.substring(index, index.length);
}

//转换数字
function formatQuantity(quantity) {
  quantity = parseInt(quantity);
  if (quantity > 100000) {
    return quantity = "10w+"
  } else if (quantity >= 90000) {
    return quantity = "9w+"
  } else if (quantity >= 80000) {
    return quantity = "8w+"
  } else if (quantity >= 70000) {
    return quantity = "7w+"
  } else if (quantity >= 60000) {
    return quantity = "6w+"
  } else if (quantity >= 50000) {
    return quantity = "5w+"
  } else if (quantity >= 40000) {
    return quantity = "4w+"
  } else if (quantity >= 30000) {
    return quantity = "3w+"
  } else if (quantity >= 20000) {
    return quantity = "2w+"
  } else if (quantity >= 10000) {
    return quantity = "1w+"
  } else {
    return quantity;
  }
}



//切割图片
function splitSourceUrl(url) {
  var urlArr = url.split('~');
  return urlArr;
}
//随机产生转发图片和文字
function randomShare() {
  var share = [{
    title: '看什么看,再看信不信我咬你啊',
    imageUrl: 'https://cmhdtstatic.oss-cn-beijing.aliyuncs.com/share_image1.jpg'
  }, {
    title: '铲屎的不听话,这日子没法过了',
      imageUrl: 'https://cmhdtstatic.oss-cn-beijing.aliyuncs.com/share_image2.jpg'
  }, {
    title: '狗奴才,还不快点来铲屎',
      imageUrl: 'https://cmhdtstatic.oss-cn-beijing.aliyuncs.com/share_image3.jpg'
  }]

  var index = Math.random() * 3;
  index = Math.floor(index);
  return share[index];
}

//转换时间
function formatDate(strDate) {
  var fullYear = parseInt(strDate.substring(0, 4));
  var month = parseInt(strDate.substring(5, 7));
  var day = parseInt(strDate.substring(8, 10));
  var hours = parseInt(strDate.substring(11, 13));
  var minutes = parseInt(strDate.substring(14, 16));
  //当前时间
  var newDate = new Date();
  var newFullYear = newDate.getFullYear();
  var newMonth = newDate.getMonth() + 1;
  var newDay = newDate.getDate();
  var newHours = newDate.getHours();
  var newMinutes = newDate.getMinutes();
  var newSeconds = newDate.getSeconds();
  //如果年月日都相同
  if (fullYear == newFullYear && month == newMonth && day == newDay) {
    return '今天'+hours + ':' + minutes
  }
  if (fullYear == newFullYear) {
    return month + '-' + day
  }
  return fullYear + '-' + month + '-' + day

}

//图片原始高,图片原始宽,缩放比例
function getImageHeight(initialHeight, initialWidth, scale) {
  //设备可用宽度
  var screenWidth = app.globalData.systemInfo.screenWidth;
  //图片实际宽度 = 屏幕宽 * 0.48
  var imgageWidth = screenWidth * scale;
  //图片原始宽和实际宽比例
  var widthScale = imgageWidth / initialWidth;
  //自适应高度
  var imageHeight = initialHeight * widthScale;
  return imageHeight;
}

module.exports = {
  getUUID: getUUID,
  getMimeType: getMimeType,
  formatQuantity: formatQuantity,
  splitSourceUrl: splitSourceUrl,
  randomShare: randomShare,
  formatDate: formatDate,
  getImageHeight: getImageHeight
}